<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>
  <body>
    <div class="container">
      <ul class="top">
        <!-- 基础模板 -->
        <!-- <li>
          <a href="javascript:;">首页</a>
          <ul class="sub">
            <li>
              <a href="javascript:;">
                <span>砂锅厨具</span>
                <img src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView" alt="">
              </a>
            </li>
          </ul>
        </li> -->
      </ul>
    </div>
    <script src="./axios.js"></script>
    <script>
      /*
        1. 获取一级标题
        2. 基于一级标题的id 获取与之对应的二级标题的数据
        3. 所有二级标题的数据拿到之后,渲染
      */
      async function getTitle() {
        // 1. 获取一级标题
        const topRes = await axios({
          url: 'http://ajax-api.itheima.net/api/category/top',
        })
        // console.log('topRes:', topRes)
        // 2.有多少个一级,发送多少个请求二级的
        // 基于数组 map 生成Promise数组 发送9个请求
        const twoPros = topRes.data.data.map(v => {
          return axios({
            url: 'http://ajax-api.itheima.net/api/category/sub',
            params: {
              id: v.id,
            },
          })
        })
        // console.log('twoPros:', twoPros)

        // 3. 所有二级标题的数据拿到之后,渲染
        const twoRes = await Promise.all(twoPros)
        // console.log('twoRes:', twoRes)
        const html = twoRes
          .map(v => {
            return `
            <li>
              <a href="javascript:;">${v.data.data.name}</a>
              <ul class="sub">
               ${v.data.data.children
                 .map(son => {
                   return `<li>
                      <a href="javascript:;">
                        <span>${son.name}</span>
                        <img src="${son.picture}" alt="">
                      </a>
                    </li>`
                 })
                 .join('')}
              </ul>
            </li>
          `
          })
          .join('')
        // console.log('html:', html)
        document.querySelector('.top').innerHTML = html
      }

      window.onload = function () {
        getTitle()
      }
    </script>
  </body>
</html>
